'use strict';

import  UserInfo from '../../business/user_info.js';
import Base from './base.js';
import DBHelper from '../../business/db_helper.js';


export default class extends Base {
    /**
     * index action
     * @return {Promise} []
     */
    indexAction() {

        if (this.isGet()) {
            //auto render template file login_index.html
            return this.display();
            return this.display();
        }
        else {
            var post = this.post();


            DBHelper.bgAccountLogin(post.account, global.hmac(post.password)).then(async rows=> {


                if (rows[0][0][0] == null) {
                    return this.display();
                }
                else {
                    var info = rows[0][0][0];
                    await this.session("userInfo", new UserInfo(info));
                    var url = this.referer();


                    if (url.indexOf("logout") == -1 && !(url.indexOf("login_log") == -1 && url.indexOf("login") != -1)) {

                        return this.redirect(url);
                    }
                    else {

                        return this.redirect("/manage");
                    }

                }
            });


        }
    }

    //用户登出
    async logoutAction() {
        await this.session();
        return this.display("index");
    }


}